<?php
	/**
	 +=========================================================
	 * 零度空间 ZD cms 网站管理系统
	 * CategoryAction.class.php 分类管理类
	 +=========================================================
	 * @copyright © 2011 nickdraw.com All rights reserved.
	 * @author NickDraw(零度温柔) webmaster@206c.net
	 * @license http://www.nickdraw.com/license
	 +=========================================================
	 */
	
	class CategoryAction extends CommonAction{
		public function index(){
			$img_path = C('TMPL_PARSE_STRING');
			$model = M($this->getActionName());
			$html = '';
			$data = $model->where("pid='0'")->order("sort")->select();
			$i = 1;
			foreach($data as $key=>$val){
				$trcolor = '';
				if ( ( $i % 2 ) == 0 ){
					$trcolor = ' bgcolor="#f9f9f9"';
				}
				
				if ($this->checkSonCategory($model, $val['id'])){
					$display = '<img src="'.$img_path['__THEMES__'].'/images/display.gif" align="absmiddle" style="cursor: pointer;" class="displayImg" rel="' . $val['id'] . '" />';
				}else{
					$display = '<img src="'.$img_path['__THEMES__'].'/images/hidden.gif" align="absmiddle" style="cursor: pointer;" class="hiddenImg" rel="' . $val['id'] . '" />';
				}
				
				$html .= '<tr rel="' . $val['id'] . '" class="row_color"'.$trcolor.'>' . "\r\n" .
					'<td align="center" rel="'.$val['id'].'" class="esort">'.$val['sort'].'</td>'."\r\n" .
					'<td align="center">' . $val['id'] . '</td>' . "\r\n" .
					'<td align="center">' . $display . '</td>' . "\r\n" .
					'<td align="left"><a href="'.U( 'edit?id='.$val['id'] ).'&tag='.$_GET['tag'].'" class="edit_category">' . $val['name'] . '</a></td>' . "\r\n" . 
					'<td align="center">' . getParentCategory($val['pid']) . '</td>' . "\r\n" . 
					'<td align="center">' . getStatus($val['status']) . '</td>' . "\r\n" .
					'<td align="center">' . $val['type'] . '</td>' . "\r\n" .
					'<td class="action_e" style="padding-left:15px;">' . "\r\n";
					if ($val['type'] != 'url' && $val['url'] != '/'){
						$html .= '<a href="'.U('addson?pid='.$val['id'].'&tag='.$_GET['tag'].'&type='.$val['type'].'&nav='.$val['nav'].'&channel='.$val['channel'].'&level='.($val['level']+1).'&lang='.$val['lang']).'" class="add_son">添加子及分类</a><span class="action_line">|</span>';
					}
					$html .= showStatus($val['status'],$val['id']) . '<span class="action_line">|</span>' . "\r\n";
					$html .= '<a href="'.U('delete?id='.$val['id']).'" title="' . $val['name'] . ' 删除 " rel="' . $val['id'] . '" class="del_category">删除</a>' . "\r\n".
					'</td>' . "\r\n".
				'</tr>' . "\r\n";
				
				$html .= $this->getSonCategory($model, $val['id'], '├──');
				$i++;
			}
			Cookie::set ( '_currentUrl_', __SELF__ );
            
            
			$this->assign("list",$html);
			$this->display();
		}

		public function _before_add(){
            $type_model = M('Category_type');
            $type_list = $type_model->where("status=1")->select();
            $this->assign("type_list",$type_list);
        }
		
		protected function getSonCategory($model,$pid,$step){
			$img_path = C('TMPL_PARSE_STRING');
			$data = $model->where("pid='".$pid."'")->select();
			$html = '';
			if (!empty($data)){
				$i = 1;
				foreach ($data as $key => $val) {
					$trcolor = '';
					if ( ( $i % 2 ) == 0 ){
						$trcolor = ' bgcolor="#f9f9f9"';
					}
					
					if ($this->checkSonCategory($model, $val['id'])){
						$display = '<img src="'.$img_path['__THEMES__'].'/images/display.gif" align="absmiddle" style="cursor: pointer;" class="displayImg" rel="' . $val['id'] . '" />';
					}else{
						$display = '<img src="'.$img_path['__THEMES__'].'/images/hidden.gif" align="absmiddle" style="cursor: pointer;" class="hiddenImg" rel="' . $val['id'] . '" />';
					}
					
					$html .= '<tr rel="' . $val['id'] . '" style="display:none;" class="tr_' . $val['pid'] . ' row_color"'.$trcolor.'>' . "\r\n" .
						'<td align="center" rel="'.$val['id'].'" class="esort">'.$val['sort'].'</td>' . "\r\n" .
						'<td align="center">' . $val['id'] . '</td>' . "\r\n" .
						'<td align="center"></td>' . "\r\n" .
						'<td align="left">' . $step . ' ' . $display . ' <a href="'.U( 'edit?id='.$val['id'].'&tag='.$_GET['tag'] ).'" class="edit_category">' . $val['name'] . '</a></td>' . "\r\n" . 
						'<td align="center">' . getParentCategory($val['pid']) . '</td>' . "\r\n" . 
						'<td align="center">' . getStatus($val['status']) . '</td>' . "\r\n" .
						'<td align="center">' . $val['type'] . '</td>' . "\r\n" .
						'<td class="action_e" style="padding-left:15px;">' . "\r\n";
						if ($val['type'] != 'url' && $val['url'] != '/'){
							$html .= '<a href="'.U('addson?pid='.$val['id'].'&tag='.$_GET['tag'].'&type='.$val['type'].'&nav='.$val['nav'].'&channel='.$val['channel'].'&level='.($val['level']+1).'&lang='.$val['lang']).'" class="add_son">添加子及分类</a><span class="action_line">|</span>';
						}
						$html .= showStatus($val['status'],$val['id']) . '<span class="action_line">|</span>' . "\r\n".
						'<a href="'.U('delete?id='.$val['id']).'" title="' . $val['name'] . ' 删除 " rel="' . $val['id'] . '" class="del_category">删除</a>' . "\r\n".
						'</td>' . "\r\n".
					'</tr>' . "\r\n";
					
					$html .= $this->getSonCategory($model, $val['id'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$step);
					$i++;
				}
			}
			return $html;
		}
		
		protected function checkSonCategory($model,$pid){
			$data = $model->where("pid='".$pid."'")->select();
			if (!empty($data)){
				return true;
			}else{
				return false;
			}
		}
		
		public function updateOneSort(){
        	$model = D('Category');
        	$id = trim( $_POST['id'] );
        	$sort = trim( $_POST['sort'] );
        	if ( !empty( $sort ) && is_numeric( $sort ) ){
        		$model->where("id='".$id."'")->setField('sort',$sort);
        	}
        }
		
		public function otheraction(){
			if ($_POST['update_sort']){
				$model = D('Category');
				foreach($_POST['sort_list'] as $key=>$val){
					$model->where("id='".$key."'")->setField('sort',$val);
				}
				$this->assign("jumpUrl", Cookie::get ( '_currentUrl_' ) );
				$this->success ('更新完成!');
			}
		}

	}
	